﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace LifeRestartConfigTool
{
    /// <summary>
    /// txt(.cfg)配置表转js
    /// </summary>
    public class ConfigAnalyzer
    {
        private static Regex cfgRegex = new Regex("\\w+\\.cfg");

        /// <summary>
        /// 解析当前文件夹中所有.cfg文件
        /// </summary>
        public static void AnalysisCurrentFolder()
        {
            string[] files = Directory.GetFiles(Directory.GetCurrentDirectory());
            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                if (cfgRegex.IsMatch(fileName))
                {
                    try
                    {
                        AnalysisFile(file, file.Replace(".cfg", ".js"));
                    }
                    catch (Exception e)
                    {

                    }
                }
            }
        }

        /// <summary>
        /// 执行解析单个文件
        /// </summary>
        /// <param name="path">文件路径</param>
        public static void AnalysisFile(string fromPath, string targetPath)
        {
            Console.WriteLine($"解析{fromPath}");
            Console.WriteLine($"目标{targetPath}");
            string jsStr = StringToJs(FileUtil.ReadFromFile(fromPath), Path.GetFileName(fromPath).Split(".")[0]);
            FileUtil.WriteToFile(targetPath, jsStr);
        }

        /// <summary>
        /// string转js
        /// </summary>
        /// <param name="str">待转化字符串</param>
        private static string StringToJs(string str, string fileName = "配置类名字")
        {
            StringBuilder res = new StringBuilder();
            res.Append("export const " + fileName + " = {\n");

            string[] lines = str.Split("\r\n");
            //第一行关键字行
            string[] keys = lines[0].Split("\t");
            //第三行数据类型行
            string[] types = lines[2].Split("\t");
            int index = 0;
            for (int i = 3; i < lines.Length; i++)
            {
                if ("".Equals(lines[i])) continue;

                //按制表符拆分
                string[] tabLines = lines[i].Split("\t");
                if (tabLines.Length > 0)
                {
                    //js单个对象开始
                    res.Append(string.Format("'{0}':{1}\n", index, "{"));
                    for (int j = 0; j < tabLines.Length; j++)
                    {
                        if ("".Equals(tabLines[j])) continue;

                        switch (types[j])
                        {
                            case "1":
                                //直接添加
                                res.Append($"{keys[j]}: {tabLines[j]},\n");
                                break;
                            case "2":
                                //加引号
                                res.Append($"{keys[j]}: '{tabLines[j]}',\n");
                                break;
                            case "3":
                                {
                                    //一维数组
                                    string[] arrOne = tabLines[j].Split("#");
                                    if (arrOne.Length > 0)
                                    {
                                        res.Append($"{keys[j]}: [" + arrOne[0]);
                                        for (int l = 1; l < arrOne.Length; l++)
                                        {
                                            res.Append($",{arrOne[l]}");
                                        }
                                        res.Append("],\n");
                                    }
                                    break;
                                }
                            case "4":
                                {
                                    //二维数组
                                    string[] arrOne = tabLines[j].Split("|");
                                    res.Append($"{keys[j]}: [\n");
                                    for (int k = 0; k < arrOne.Length; k++)
                                    {
                                        string[] arrTwo = arrOne[k].Split("#");
                                        if (arrTwo.Length > 0)
                                        {
                                            res.Append("[" + arrTwo[0]);
                                            for (int l = 1; l < arrTwo.Length; l++)
                                            {
                                                res.Append($",{arrTwo[l]}");
                                            }
                                            res.Append("],\n");
                                        }
                                    }
                                    res.Append("],\n");
                                    break;
                                }
                            default:
                                res.Append($"{keys[j]}: {tabLines[j]},\n");
                                break;
                        }
                    }
                    //js单个对象结束
                    res.Append("},\n");
                    index++;
                }
            }
            res.Append("}");

            return res.ToString();
        }
    }
}
